<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="输入搜索" prop="name">
        <el-input v-model="queryParams.content" placeholder="区域位置/人员姓名/车辆名称/车牌号码" clearable style="width: 300px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd()" class="manage-add-btn">添加区域</el-button>
    </el-form>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column label="区域名称" align="center" prop="name" />
      <el-table-column label="作业人员" align="center">
        <template slot-scope="{ row }">
          <span v-for="item in row.employees" :key="item.id">{{ item.name }}、</span>
        </template>
      </el-table-column>
      <el-table-column label="作业车辆" align="center">
        <template slot-scope="{ row }">
          <span v-for="item in row.cars" :key="item.id">{{ item.name }}、</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="bandding(scope.row.id)">绑定人员
          </el-button>
          <el-button size="mini" type="text" @click="boundCars(scope.row.id)">绑定车辆</el-button>
          <el-button size="mini" type="text" @click="handleView(scope.row.id)">查看</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { arealist, delRegion } from "@/api/system/area";

export default {
  name: "Operlog",
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请填写姓名', trigger: 'blur' },
        ],
        userNumber: [
          { required: true, message: '请填写用户ID', trigger: 'blur' },
        ],
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 表单参数
      form: { title: '添加人员', userNumber: '', name: '' },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: undefined,
      },
      hireDate: '',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志 */
    getList() {
      this.loading = true;
      arealist(this.addDateRange(this.queryParams)).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      }
      );
    },
    // 查看
    handleView(id) {
      this.$router.push({ path: `/work/region/addRegion?id=${id}` })
    },
    //删除区域
    handleDelete(id) {
      this.$confirm('此操作将删除该区域, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRegion(id).then(response => {
          this.getList();
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //绑定员工
    bandding(id) {
      this.$router.push({ path: `/work/region/employees?areaId=${id}` })
    },
    //绑定车辆
    boundCars(id) {
      this.$router.push({ path: `/work/region/boundCars?areaId=${id}` })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAdd() {
      this.$router.push({ path: '/work/region/addRegion' })
    },
    submit() {
      this.form.hireDate = this.moment(this.hireDate).format("YYYY-MM-DD")
      addEmployee(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.manage-add-btn {
  float: right;
}
</style>
